﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using control.Biz;
using System.Collections.Generic;

namespace TrangSucShop.usercontrol
{
    public partial class categories : System.Web.UI.UserControl
    {
        #region property phan trang

        private int CurrentPage // thuoc tinh int currentpage chi ra trang hien tai minh dang su~ dung vd trang 1 2 3 ..
        {
            get // get lay ra khi nguoi ta su dung x = CurrentPage
            {
                // vi khi nguoi ta bam chon trang khac  thi se~ load lai trang ma loade lai se mat thuoc tinh nen chung 
                //ta phai truyen Current page vao cookie de luu lai ko bi mat moi lan goi CurrentPage la lay cookie ra
                object objPage = ViewState["_CurrentPage"];//lay cookie viewstate
                int _CurrentPage = 0;                      //code lay gia try cookie bo vao currentpage neu ko tra ve 0
                if (objPage == null) // kiem tra xem co cookie currenpage chua neu chua co thi tra ve 0
                {
                    _CurrentPage = 0;
                }
                else
                {
                    _CurrentPage = (int)objPage;
                }
                return _CurrentPage;// tra ve 1 gia tri int 
            }
            set { ViewState["_CurrentPage"] = value; } // set thi se set gia tri cho cookie de luu lai cho den khi tat trinh duyet moi het
        }
        private int fistIndex // tuong tu currenpage
        {
            get//code lay gia try cookie bo vao firstindex neu ko tra ve 0
            {

                int _FirstIndex = 0;
                if (ViewState["_FirstIndex"] == null)
                {
                    _FirstIndex = 0;
                }
                else
                {
                    _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
                }
                return _FirstIndex;
            }
            set { ViewState["_FirstIndex"] = value; }
        }
        private int lastIndex
        {
            get//code lay gia try cookie bo vao lastindex neu ko tra ve 0
            {

                int _LastIndex = 0;
                if (ViewState["_LastIndex"] == null)
                {
                    _LastIndex = 0;
                }
                else
                {
                    _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
                }
                return _LastIndex;
            }
            set { ViewState["_LastIndex"] = value; }
        }

        #endregion
        #region property usercontrol

        private List<BizSanPham> data;
        public List<BizSanPham> Data
        {
            get
            {
                if (data == null)
                    data = new List<BizSanPham>();
                return data;
            }
            set { data = value; }
        }

        private string tieude;
        public string Tieude
        {
            set { tieude = value; }
            get { return tieude; }
        }
        #endregion
        #region code phan trang
        PagedDataSource _PageDataSource = new PagedDataSource(); // bien phan trang
        private void BindItemsList() // code de hien thi trang chung ta chon
        {
            Label1.Text = Tieude;
            _PageDataSource.DataSource = Data; // set data suorce cho bien phan trang = data
            _PageDataSource.AllowPaging = true; // cho phep phan trang neu de false se ko phan trang
            _PageDataSource.PageSize = 6; // so sp moi trang 
            _PageDataSource.CurrentPageIndex = CurrentPage; // trang chung ta muon hien thi
            ViewState["TotalPages"] = _PageDataSource.PageCount; // set cookie tong so trang = _PageDataSource.PageCount
            //_PageDataSource.PageCount duoc tinh nhu the nay`: neu co 4 sp ma` pagesize = 3 thi pagecount = 2
            //neu co 10sp ma pagesize = 2 thi pagecount = 5.

            //this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
            this.LbtPre.Enabled = !_PageDataSource.IsFirstPage; //hay hay thay cho if 
            //sua lai thanh lbt cua minh
            // cai nay thay cho if(dang o trang dau`) thi` lbtPre se kong hoat dong
            this.LbtNext.Enabled = !_PageDataSource.IsLastPage;
            this.LbtFirst.Enabled = !_PageDataSource.IsFirstPage;
            this.lbtLast.Enabled = !_PageDataSource.IsLastPage;// 3 cai nay tuong tu chi khac cho lastpage voi firstpage thoi
            // neu ma la trang dau thi khong the dung < or <<
            //neu la trang cuoi thi ko the dung > or >>
            //phai doi ten datalist lai cho dung voi bai` cua minh
            this.DataList1.DataSource = _PageDataSource; // gang datasource cho datalist1 ( datalist hien thi sp)
            this.DataList1.DataBind(); // bind no
            this.doPaging();// day la code he hien thi so trang minh` co o datalist2 ( datalist ben duoi')
        }

        private void doPaging()//code he hien thi so trang minh` co o datalist2
        {
            DataTable dt = new DataTable(); // tao 1 cai datatable cai nay giong cai list thoi
            dt.Columns.Add("PageIndex");//add cot hien thi gia tri index cua trang cai nay la day so tu 0 toi n
            dt.Columns.Add("PageText");//add cot hien thi ten trang vd 0 1 2 3 4 hay trang 1, trang 2, trang 3 hay la hinh anh tuy y
            fistIndex = 0;//truoc tien gang trang dau = 0 khi gang se chuyen vao cookie 
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]); //trang cuoi se = cookie totalpage ma chung ta da gang
            //o phan binding() brn tren
            #region day la code su dung de quy dinh moi lan hien thi chi hien thi 10 trang vd << < 5->15 > >> co the dung hoach ko can
            //fistIndex = CurrentPage - 5;
            //if (CurrentPage > 5)
            //{
            //    lastIndex = CurrentPage + 5;
            //}
            //else
            //{
            //    lastIndex = 10;
            //}
            //if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
            //{
            //    lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            //    fistIndex = lastIndex - 10;
            //}
            //if (fistIndex < 0)
            //{
            //    fistIndex = 0;
            //}
            #endregion

            for (int i = fistIndex; i < lastIndex; i++) // dung vong for de tao gia tri cho datatable
            {                                           //vd ta co 5 trang thi se thiet lap 5 gia tri 0 1 2 3 4 cho index
                DataRow dr = dt.NewRow();               // va 1 2 3 4 5 cho text (chung ta co the doi thanh trang1,trang2...)
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }
            this.DataList2.DataSource = dt;//set datasource cho datalist2 (datalist ben duoi)
            this.DataList2.DataBind();      // nhu vay neu chung ta co totalpage = 5 trang thi datalist se hien thi 1,2,3,4,5
        }
        #endregion
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                BindItemsList();

        }

        protected void LbtFirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 0;//set lai curr = 0 tuc la trang dau
            BindItemsList();//bind lai de hien thi trang hien hanh`
        }

        protected void lbtLast_Click(object sender, EventArgs e)
        {
            CurrentPage = Convert.ToInt32(ViewState["TotalPages"]) - 1; // trang hien hanh = trang cuoi
            BindItemsList();
        }

        protected void LbtNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1; // trang ke
            BindItemsList();
        }

        protected void LbtPre_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            BindItemsList();
        }

        protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("page"))//kiem tra ten cua command 
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());//set currentpage = agument chung ta truyen vao
                this.BindItemsList();                                       //cai phan quan trong hoi nay
            }
        }

        protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lbtPage");// tim` ra cai nut  = id cua linkbutton
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())//kiem tra xem nut do co phai la nut 
            {                                                                   //cua trang hien tai ko
                lnkbtnPage.Enabled = false;// vo hieu hoa no
                lnkbtnPage.Style.Add("fone-size", "14px"); //chinh style cho no co the them mau` me` tuy thich
                lnkbtnPage.Font.Bold = true;//to dam no len

            }
        }


    }
}